package com.lingnan.controller;


import com.lingnan.pojo.User;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.pojo.Active;
import com.lingnan.pojo.Collect;
import com.lingnan.service.ActiveService;
import com.lingnan.service.CollectService;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@RestController
@CrossOrigin
@RequestMapping("/collect")
public class CollectController {


    @Autowired
    CollectService collectService;
    @Autowired
    ActiveService activeService;

    /**
     * 根据用户ID查询收藏表
     *
     * @return
     */
    @PostMapping("/collectList")
    @ApiOperation("根据用户id展示收藏表")
    public Result collectList(@RequestBody UserVo userVo) {
        if (!ObjectUtils.isEmpty(userVo)) {
            if (!ObjectUtils.isEmpty(userVo.getNo())) {
                List<Active> activeList = collectService.collectList(userVo.getNo());
                return ResultGenerator.genSuccessResult(200, activeList, "收藏表展示成功");
            }
            return ResultGenerator.genErrorResult(201, "参数错误，收藏表展示失败");
        }
        return ResultGenerator.genErrorResult(201, "参数错误，收藏表展示失败");
    }

    /**
     * 添加活动到收藏
     *
     * @param collect
     * @return
     */
    @PostMapping("/addCollect")
    @ApiOperation("用户将某个活动添加收藏")
    public Result addCollect(@ApiParam("收藏表(活动id、学号no、是否收藏(0为收藏)、收藏时间(collectTime))") @RequestBody Collect collect) {
        if (!ObjectUtils.isEmpty(collect)) {
            //判断要收藏的活动是否存在
            Active active = activeService.getById(collect.getActiveId());
            if (!ObjectUtils.isEmpty(collect.getActiveId())
                    && !ObjectUtils.isEmpty(collect.getNo())
                    && !ObjectUtils.isEmpty(collect.getCollectTime())
                    && !ObjectUtils.isEmpty(collect.getUseful())
                    && !ObjectUtils.isEmpty(active)) {
                //查看改活动是否之前被收藏过
                Collect collectServiceById = collectService.selectByIdAndNo(collect.getActiveId(), collect.getNo());
                if (!ObjectUtils.isEmpty(collectServiceById)) {
                    //该活动曾经被添加到收藏夹，进行逻辑添加
                    boolean flag = collectService.updateUseful(collect.getActiveId(), collect.getNo());
                    if (flag) {
                        return ResultGenerator.genSuccessResult(200, "活动收藏成功");
                    }
                } else {
                    //该活动之前未被添加到收藏夹，进行插入
                    boolean save = collectService.save(collect);
                    if (save) {
                        return ResultGenerator.genSuccessResult(200, "活动收藏成功");
                    }
                }
            }
        }
        return ResultGenerator.genErrorResult(201, "活动收藏失败");
    }

    @DeleteMapping("/deleteCollect")
    @ApiOperation("将活动从收藏表中移除(逻辑删除)")
    public Result deleteCollect(@RequestBody Collect collect) {
        if (!ObjectUtils.isEmpty(collect)) {
            if (!ObjectUtils.isEmpty(collect.getActiveId()) && !ObjectUtils.isEmpty(collect.getNo())) {
                boolean flag = collectService.deleteCollect(collect.getActiveId(), collect.getNo());
                if (flag) {
                    return ResultGenerator.genSuccessResult(200, "活动移除成功");
                }
            }
        }
        return ResultGenerator.genErrorResult(201, "该活动移除失败");
    }
}

